/**
 * 类式组件
 */
import React from 'react';
import {Divider, Form, Input, message, Modal, Radio} from "antd";
import axios from "axios";
import servicePath from "../config/apiConfig";
class AddStudentClass extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            isModalVisible: props.isVisible
        };
        this.closeModal= props.closeModal;
        this.formRef = React.createRef();
    }
    oncloseModal = ()=>{
        this.setState({
            isModalVisible: false
        })
        this.closeModal()
    }
    onsubmit = ()=>{
        const that = this
        this.formRef.current.validateFields().then(value => {
            axios.post(servicePath.addStudent, {...value}).then(res=>{
                const data = res.data
                if(data.code === 200) {
                    message.success('录入学生信息成功！',1);
                    that.oncloseModal()
                } else {
                    message.error('录入学生信息失败！',1);
                }
            }).catch(err=>{
                message.error(err,1);
            })
        }).catch(err=>{
            console.log(err)
        })
    }

    render() {
        const layout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 10 },
        };
        const {isModalVisible} = this.state
        const {studentInfoForm, oncloseModal, onsubmit} = this
        return (
            <div>
                <Modal title="更新学生信息" visible={isModalVisible} onOk={onsubmit} onCancel={oncloseModal}>
                    <Form {...layout} form={studentInfoForm} name="add-student" ref={this.formRef}>
                        <Divider orientation="left">基本信息</Divider>
                        <Form.Item name="snumber" label="学号" rules={[{ required: true, message: '学号不能为空!' }]}>
                            <Input placeholder="请输入学号"/>
                        </Form.Item>
                        <Form.Item name="name" label="姓名" rules={[{ required: true, message: '姓名不能为空!' }]}>
                            <Input placeholder="请输入姓名"/>
                        </Form.Item>
                        <Form.Item name="classes" label="班级" rules={[{ required: true, message: '班级不能为空!' }]}>
                            <Input placeholder="请输入班级"/>
                        </Form.Item>
                        <Form.Item name="sex" label="性别" rules={[{ required: true, message: '性别不能为空!' }]}>
                            <Radio.Group>
                                <Radio value="男">男</Radio>
                                <Radio value="女">女</Radio>
                            </Radio.Group>
                        </Form.Item>
                        <Divider orientation="left">各科成绩</Divider>
                        <Form.Item name="cscore" label="c语言">
                            <Input placeholder="请输入c语言成绩"/>
                        </Form.Item>
                        <Form.Item name="jscore" label="java">
                            <Input placeholder="请输入java成绩"/>
                        </Form.Item>
                        <Form.Item name="dbscore" label="数据库">
                            <Input placeholder="请输入数据库成绩"/>
                        </Form.Item>
                    </Form>
                </Modal>
            </div>
        )
    }
}

export default AddStudentClass

